<template>
  <div class="flex">
    <a-button @click="download"> {{ t["下载模板"] }} </a-button>
    <a-button @click="upload"> {{ t[uploadText] }} </a-button>
  </div>
  <a-modal v-model:visible="resultModel" title="结果">
    <UploadResult v-bind="batchAddTagResult" />
  </a-modal>
</template>

<script setup lang="ts">
import { fileTemplate, updateFile } from "@/api/service/utils";

import { useUploadFile } from "@/utils/file";
import { Message } from "@arco-design/web-vue";

const t = translate("公共模块");
const batchAddTagResult = ref();
const resultModel = ref();
const props = defineProps<{
  downloadLink: string;
  uploadLink: string;
  uploadText: string;
  uploadType: number | string;
  downloadType?: number | string;
}>();

const upload = async () => {
  const file = await useUploadFile(".xls,.xlsx");
  const { data } = await updateFile(file, props.uploadLink, `${props.uploadType}`);

  if (data.code === 200) {
    if (props.uploadLink === `/priceTag/priceTag/warehouse/xlsx`) {
      batchAddTagResult.value = data.data;
      resultModel.value = true;
      return;
    }
    const { msg } = data;
    Message.info(`${msg}`);
  } else {
    Message.warning(data.msg);
  }
};

const download = async () => {
  await fileTemplate(props.downloadLink, {
    type: props.downloadType
  });
  Message.success("success");
};
</script>

<style lang="scss" scoped></style>
